package cn.net.xyan.easy.gwt.client.ui.cell;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import org.gwtbootstrap3.client.ui.constants.ButtonType;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.gwt.ButtonCell;

/**
 * Created by zarra on 16/3/3.
 */
public abstract class ButtonItemCell<T> implements HasCell<T,String> {

    ButtonType buttonType = ButtonType.DEFAULT;

    IconType iconType = IconType.ARCHIVE;

    String title;

    ButtonCell buttonCell = null;

    public ButtonType getButtonType() {
        return buttonType;
    }

    public void setButtonType(ButtonType buttonType) {
        this.buttonType = buttonType;
    }

    public IconType getIconType() {
        return iconType;
    }

    public void setIconType(IconType iconType) {
        this.iconType = iconType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ButtonItemCell(String title, ButtonType buttonType, IconType iconType){
        setTitle(title);
        setButtonType(buttonType);
        setIconType(iconType);
    }

    @Override
    public Cell<String> getCell() {
        if (buttonCell == null)
            buttonCell = new ButtonCell(getButtonType(), getIconType());
        return buttonCell;
    }

    @Override
    public FieldUpdater<T, String> getFieldUpdater() {
        return new FieldUpdater<T, String>() {
            @Override
            public void update(int index, T object, String value) {
                onAction(index,object,value);
            }
        };
    }

    @Override
    public String getValue(T object) {
        return title;
    }

    public abstract void onAction(int index, T object, String value);
}
